/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


var fristTitle = "file__1";

var thisfiles = fristTitle;
var myui = null;

var thisfileMmodel = fristTitle;


function filecreate(ui, fileName, fileName2, graph, myitems) {

    if (fileName == "") {
        fileName = fristTitle;
    }
    fileUpdate(ui, graph);
    thisfiles = fileName;
    thisfileMmodel = fileName2;


    getSesstion("directedModefiles", function(mvalue) {
        var fileNames = mvalue.split(";");
        if (fileNames.indexOf(fileName) == -1) {
            setSesstion(fileName + "_mmodle", fileName2);
            setSesstion("directedModefiles", mvalue + ";" + fileName);

            graphClear(graph);


        } else {
            graphUpdate(ui, graph);
        }
    });

}



function fileUpdate(ui, graph) {
    // alert(thisfileMmodel + ":::::" + saveDirectionXmlData(graph));

    setSesstion(thisfiles, Ahtml(mxUtils.getPrettyXml(ui.editor.getGraphXml())));
    setSesstion(thisfiles + "_data", Ahtml(saveDirectionXmlData(graph)));


}
function graphUpdate(ui, graph) {


    getSesstion(thisfiles, function(data) {

        var doc = mxUtils.parseXml(Chtml(data));
        ui.editor.setGraphXml(doc.documentElement);

        reUpdateSildebar(graph);
    });

}


function graphClear(graph) {
    graph.removeCells(graph.getChildVertices(graph.getDefaultParent()))

    reUpdateSildebar(graph);
}




function saveDirectionXmlData(graph) {

    var dta = thisfiles.split("__");
    var cells = getBCells(graph);


    var value = '<direction from-system="' + $.trim(dta[0]) + '" to-system="' + $.trim(dta[1]) + '" >\n';
    for (var i = 0; i < cells.length; i++) {
        if (cells[i] != null) {
            var v = cells[i].getValue();
            var con = getEdgesForCell(cells[i].getId());


            var val = v.replace(" ", "");
            value += '<directed-migration item="' + v + '"> \n';

            var vdata = $(".itmedata ." + $.trim(val) + "_data." + thisfiles).val();


            //  alert(".itmedata ." + $.trim(val) + "_data." + thisfiles + ":::" + vdata);
            if (vdata == "null" || vdata == null) {


                vdata = thisfileMmodel;

            }
            value += '<migration-model ref="' + vdata + '"/> \n';

            if (con != null && con != "") {

                var mycon = con.split("__");
                for (var n = 0; n < mycon.length; n++) {
                    if (mycon[n] != null && mycon[n] != "") {
                        var mycwidthOut = mycon[n].replace(" ", "");
                        var vwidthOut = v.replace(" ", "");

                        value += '<directed-depends-on item="' + mycon[n] + '" from-association="Many' + vwidthOut + 'to' + mycwidthOut + '"/>\n';

                    }
                }
            }

        }

        value += '</directed-migration>\n';


    }
    value += '</direction>\n';

    return value;


}

function returnfileName(file) {

    return  $.trim(file.split(";")[0]) + "__" + $.trim(file.split(";")[1]);

}


function filesSaveDialogs(ui, files, graph, myitems) {



    var updateFiles = [];
    var updateFilestext = [];

    for (var i = 0; i < files.length; i++) {

        if (files[i] != null && files[i] != "") {

            if ($.trim(files[i].split(";")[0]) != "null"
                    && $.trim(files[i].split(";")[0]) != "" &&
                    $.trim(files[i].split(";")[1]) != "null") {
               

                updateFiles.push($.trim(files[i].split(";")[0]) + "__" + $.trim(files[i].split(";")[1]));
                updateFilestext.push("From " + files[i].split(";")[0] + " to " + files[i].split(";")[1]);

            }




        }
    }



    var div = document.createElement('div');
    div.setAttribute('align', 'left');
    div.appendChild(createItem('Select Directed Model', "", "thtitle"));
    var table = document.createElement('table');
    table.style.width = '100%';
    table.style.height = '100%';


    var tbody = document.createElement('tbody');
    row = document.createElement('tr');
    var tds = createLabel('Title');
    row.appendChild(tds);

    row.appendChild(tds);
    tbody.appendChild(row);
    row = document.createElement('tr');

    tds = createSelect("", "fileName", "", updateFiles, updateFilestext);

    row.appendChild(tds);
    tbody.appendChild(row);

    row = document.createElement('tr');
    tds = createLabel('migration-model', "2");
    row.appendChild(tds);
    tbody.appendChild(row);

    row = document.createElement('tr');
    tds = createSelect("", "myitemdata", "", ["Src2TgtStateModel", "Tgt2SrcStateModel", "ForceErrorStateModel"], ["Src2TgtStateModel", "Tgt2SrcStateModel", "ForceErrorStateModel"]);
    row.appendChild(tds);
    tbody.appendChild(row);


    table.appendChild(tbody);
    div.appendChild(table);

    var myUp = div.appendChild(mxUtils.button("Open", function()

    {
        if ($(".fileName").val() != null) {



            filecreate(ui, $(".fileName").val(), $(".myitemdata").val(), graph, myitems);
            ui.hideDialog();
        } else {


            alert("There is no <direction> tag defined. \n Please make a <direction>  in Topology.")
        }

    }));

    myUp.setAttribute('class', 'myupdate');
    div.appendChild(myUp);

    this.container = div;
}




function getfile(th) {
    fileUpdate(myui, graph);
    thisfiles = th;
    graphUpdate(myui);
    myui.hideDialog();


}